import React from 'react';
import {Route, Switch} from 'react-router';
import {Link, HashRouter,Redirect,withRouter,BrowserRouter} from 'react-router-dom'
import Home from '../containers/Home';
import Detail from '../containers/Detail';
import City from '../containers/City';
import NotFound from '../containers/NotFound';
import User from "../containers/User"
import Search from '../containers/Search'
import LocalStore from '../util/localStore'
import {CITYNAME} from '../config/localStoreKey'

import {bindActionCreators} from 'redux'
import {connect} from 'react-redux'
import * as userInfoActionsFormOtherFile from '../actions/userinfo'
class RouteMap extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      initDone: false
    }
  }

  render() {
    return (
      <BrowserRouter>
        <div>
          {
            this.state.initDone ?
              <Switch>
                <Route exact path="/" component={Home}/>
                <Route exact path="/city" component={City}/>
                <Route exact path="/user" component={User}/>
                <Route exact path="/search/:type(/:keyword)" component={Search}/>
                <Route path="/detail/:id" component={Detail}/>
                <Route component={NotFound}/>
                <Redirect from='/ccc' to='/city'/>
              </Switch>
              : <div>loading...</div>
          }
        </div>
      </BrowserRouter>
    )
  }
  componentDidMount(){
    console.log(CITYNAME);
    let cityName = LocalStore.getItem(CITYNAME);
    if(cityName ==null){
      cityName="杭州"
    }
console.log(cityName);
    // 第四步:触发函数
    console.log(this.props);
    this.props.userInfoActions.login({
      cityName:cityName
    });
    setTimeout(()=>{
      this.setState({
        initDone:true
      })
    },1000)
  }

}

function mapStateToProps(state) {
  return {
    userinfo:state.userinfo
  }
}

function mapDispatchToProps(dispatch) {
  return {
    userInfoActions:bindActionCreators(userInfoActionsFormOtherFile,dispatch)
  }
}
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(RouteMap)

































